package com.ITXu.reggie.service.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.ITXu.reggie.commom.CustomException;
import com.ITXu.reggie.entity.Category;
import com.ITXu.reggie.entity.Dish;
import com.ITXu.reggie.entity.Setmeal;
import com.ITXu.reggie.mapper.CategoryMapper;
import com.ITXu.reggie.service.CategoryService;
import com.ITXu.reggie.service.DishService;
import com.ITXu.reggie.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService{
	
	@Autowired
	public DishService dishService;
	
	@Autowired
	public SetmealService setMealService;

	@Override
	public void remove(Long ids) {
		
		//判断判断ids是否在dishService的category_id中
		LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
		dishQueryWrapper.eq(Dish::getCategoryId, ids);
		int count1 = dishService.count(dishQueryWrapper);
		if (count1>0) {
			//抛出业务异常
			throw new CustomException("当前分类中已关联菜品，无法删除");
			//在全局异常处理器捕获业务异常
		}
		//判断ids是否在setMealService的category_id中
		LambdaQueryWrapper<Setmeal> setMealQueryWrapper = new LambdaQueryWrapper<>();
		setMealQueryWrapper.eq(Setmeal::getCategoryId, ids);
		int count2 = setMealService.count(setMealQueryWrapper);
		if (count2>0) {
			//抛出业务异常
			throw new CustomException("当前分类中已关联套餐，无法删除");
			//在全局异常处理器捕获业务异常
		}
	}

}
